iT邦幫忙

2024 iThome 鐵人賽

DAY 9
0

今天也繼續加油、繼續刷刷刷~
————————————我是可愛的題目分隔線————————————

題目1:創建集合並打印

-集合(set)是一種不包含重複元素,而且順序不固定的資料型別
-可以使用花括號 {} 或 set() 函數來創建集合

舉個例子:{}的範例
https://ithelp.ithome.com.tw/upload/images/20240923/20168371QHw6ePQuSq.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371K5UM9ifYCY.png

第1題結束。
————————————我是可愛的題目分隔線————————————

題目2:將列表轉換為集合

-使用 set() 函數將列表轉換為集合,這樣會去掉列表中的重複元素

舉個例子:set( )

(1.)創建一個空的集合

https://ithelp.ithome.com.tw/upload/images/20240923/20168371nZ58Vs1laR.png
因為目的是為了要創造空集合,所以先忽略嚴格模式下的錯誤訊息。
https://ithelp.ithome.com.tw/upload/images/20240923/20168371cvcQrcyZSU.png
(2.)將列表轉換為集合
https://ithelp.ithome.com.tw/upload/images/20240923/20168371UF4HiK7b4W.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371oo3EFstME8.png
(3.)將字串轉換為集合
https://ithelp.ithome.com.tw/upload/images/20240923/20168371eVhIHfI4Tv.png
https://ithelp.ithome.com.tw/upload/images/20240923/2016837176IpZFpcuX.png
(4.)將元組轉換為集合
https://ithelp.ithome.com.tw/upload/images/20240923/20168371rqU6TF6JzR.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371ZQeRfygTJu.png

第2題結束。
————————————我是可愛的題目分隔線————————————

題目3:檢查集合是否為子集

-使用 < 或 <= 來檢查一個集合是否完全包含在另一個集合中

舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371HMDUz7QkPR.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371fJbQWQh7mb.png

第3題結束。
————————————我是可愛的題目分隔線————————————

題目4:集合中添加和刪除元素

-添加元素:使用 add() 方法

舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371AyLmVOzTjM.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371qO799JVzS2.png

-刪除元素:使用 remove() 方法,如果元素不存在會報錯;使用 discard() 方法則不會報錯

舉個例子: remove()
https://ithelp.ithome.com.tw/upload/images/20240923/20168371NL7wjvAWAz.png
https://ithelp.ithome.com.tw/upload/images/20240923/201683715BH8jKBgWI.png

如果6不存在:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371XVC5aj6XYY.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371i3kR5YWqvW.png

舉個例子: discard()
https://ithelp.ithome.com.tw/upload/images/20240923/20168371LpwOeenMxq.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371yrZCzuqMdt.png

如果6不存在:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371RtQ3FTY48S.png
6不存在,但還是有成功印出{1, 2, 3}
https://ithelp.ithome.com.tw/upload/images/20240923/20168371COXyhqpmOj.png

第4題結束。
————————————我是可愛的題目分隔線————————————

題目5:集合去重

-集合本身會自動去重,因為集合中不能有重複的元素

舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371MOpV8QH7Vs.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371l2BbW3mStA.png

第5題結束。
————————————我是可愛的題目分隔線————————————

題目6:找出集合中的最大值和最小值

-使用 max() 和 min() 函數

舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371U5TPRpOkyy.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371KqzcKXwOcj.png

第6題結束。
————————————我是可愛的題目分隔線————————————

題目7:集合的對稱差集

  • 對稱差集是指兩個集合中的元素,去掉它們的交集部分

舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371JmUuhw8EZm.png
在集合運算中,^ 用來計算兩個集合的對稱差集,即返回兩個集合中不重疊的元素組合。
https://ithelp.ithome.com.tw/upload/images/20240923/20168371RTc2HLJX0B.png
** ^ 符號在不同的情境下有不同的意思:

  • 在位元運算中,它表示按位異或運算
  • 在集合運算中,它表示對稱差集

第7題結束。
————————————我是可愛的題目分隔線————————————

題目8:檢查兩個集合是否相等

-使用 == 來檢查兩個集合是否包含完全相同的元素

舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371bNOonxdrFu.png
https://ithelp.ithome.com.tw/upload/images/20240923/201683711d6NLT582W.png

第8題結束。
————————————我是可愛的題目分隔線————————————

題目9:集合的合集、交集和差集

-合集:合併兩個集合中的所有元素,去掉重複的
舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371aiUcc4FaAe.png
在集合運算中,| 用來計算兩個集合的並集,返回包含所有不同元素的新集合。
https://ithelp.ithome.com.tw/upload/images/20240923/201683719kEHx1Klio.png

-交集:找出兩個集合中相同的元素
舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371nn38dBHsqX.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371viywNOWI2w.png

-差集:找出在第一個集合中但不在第二個集合中的元素
舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371ch6sDlYFay.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371zllEQ87ei7.png

第9題結束。
————————————我是可愛的題目分隔線————————————

題目10:集合中隨機取樣

-可以使用 random.sample() 函數從集合中隨機取樣

舉個例子:
https://ithelp.ithome.com.tw/upload/images/20240923/20168371746ulcR2JE.png
https://ithelp.ithome.com.tw/upload/images/20240923/20168371jTNF4myRNe.png

第10題結束。
————————————我是可愛的題目分隔線————————————


上一篇
第八天刷題
下一篇
第十天刷題
系列文
Python 新手入門:挑戰30天刷完300題13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言